//
//  GroupDetailViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/1.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "GroupDetailViewController.h"
#import "FriendIDCheckViewController.h"
#import "GroupIDCheckViewController.h"
#import "GroupMemeberViewController.h"

@interface GroupDetailViewController ()

@property (nonatomic, strong) UIScrollView *backGroupScrollView; // 背景scrollview
@property (nonatomic, strong) UIImageView *topImageV; // 顶部背景图片
@property (nonatomic, strong) UILabel *groupName; // 小组名字
@property (nonatomic, strong) UIImageView *groupImage; // 小组头像
@property (nonatomic, strong) UILabel *groupID; // 小组账号
@property (nonatomic, strong) UILabel *groupMark; // 小组标签
@property (nonatomic, strong) UILabel *groupMemberCount; // 组内成员人数
@property (nonatomic, strong) UILabel *groupCreateTime; // 小组创建时间
@property (nonatomic, strong) UILabel *groupIntroduceDetail; // 小组介绍详情
@property (nonatomic, strong) UIButton *joinGroupButton; // 申请入组button

@property (nonatomic, strong) NSMutableArray *groupMemberArray; // 组内成员信息

@end

@implementation GroupDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
   
    
    // 构造测试信息
    self.groupMemberArray = [NSMutableArray arrayWithObjects:@{@"image":@"", @"name":@"张国文"}, @{@"image":@"", @"name":@"张慧杰"}, @{@"image":@"", @"name":@"张锦"}, @{@"image":@"", @"name":@"施春伟"}, @{@"image":@"", @"name":@"王娴"}, nil];
    // 添加顶部背景
    [self addTopImageV];
}

// 添加顶部背景
- (void)addTopImageV
{
    // 需要添加一个无用的leftBar
    UIImageView *leftImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageV.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageV];
    
    
    self.backGroupScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -20 * kAdjustParamater, kScreenWidth, kScreenHeight)];
    self.backGroupScrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight);
    self.backGroupScrollView.showsHorizontalScrollIndicator = NO;
    self.backGroupScrollView.showsVerticalScrollIndicator = NO;
    self.backGroupScrollView.bounces = NO;
    [self.view addSubview:_backGroupScrollView];
    
    // 顶部图片
    self.topImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 290 * kAdjustParamater)];
    self.topImageV.userInteractionEnabled = YES;
    self.topImageV.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
    [self.backGroupScrollView addSubview:_topImageV];
    
    // 添加返回按钮
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 30 * kAdjustParamater, 30 * kAdjustParamater, 30 * kAdjustParamater)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    leftImageView.userInteractionEnabled = YES;
    [self.topImageV addSubview:leftImageView];
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnToLastPage:)];
    [leftImageView addGestureRecognizer:tapGes];
    
    // 添加小组名字
    self.groupName = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 200 * kAdjustParamater) / 2, 33 * kAdjustParamater, 200 * kAdjustParamater, 22 * kAdjustParamater)];
   // self.groupName.backgroundColor = [UIColor redColor];
    self.groupName.text = @"我到台北来看雨";
    self.groupName.textAlignment = NSTextAlignmentCenter;
    self.groupName.textColor = [UIColor whiteColor];
    self.groupName.font = [UIFont systemFontOfSize:18 * kAdjustParamater];
    [self.topImageV addSubview:_groupName];
    
    // 添加小组头像
    self.groupImage = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - 100 * kAdjustParamater) / 2 , 90 * kAdjustParamater, 100 * kAdjustParamater, 100 * kAdjustParamater)];
    self.groupImage.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.groupImage.layer.cornerRadius = 5 * kAdjustParamater;
    self.groupImage.layer.masksToBounds = YES;
    [self.topImageV addSubview:_groupImage];
    
    // 小组账号
    self.groupID = [[UILabel alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, self.groupImage.bottom + 25 * kAdjustParamater, 150 * kAdjustParamater, 15 * kAdjustParamater)];
   // self.groupID.backgroundColor = [UIColor redColor];
    self.groupID.text = @"123456789";
    self.groupID.textAlignment = NSTextAlignmentLeft;
    self.groupID.textColor = [UIColor whiteColor];
    self.groupID.font = [UIFont systemFontOfSize:16 * kAdjustParamater];
    [self.topImageV addSubview:_groupID];
    
    // 小组标签
    self.groupMark = [[UILabel alloc] initWithFrame:CGRectMake(self.groupID.left, self.groupID.bottom + 8 * kAdjustParamater, 300 * kAdjustParamater, 15 * kAdjustParamater)];
   // self.groupMark.backgroundColor = [UIColor redColor];
    self.groupMark.text = @"#健康达人#我爱看电影#";
    self.groupMark.textColor = [UIColor whiteColor];
    self.groupMark.textAlignment = NSTextAlignmentLeft;
    self.groupMark.font = [UIFont systemFontOfSize:14 * kAdjustParamater];
    [self.topImageV addSubview:_groupMark];
    
    // 组内成员
    UIView *zuneiView = [[UIView alloc] initWithFrame:CGRectMake(0, self.topImageV.bottom, kScreenWidth, 35 * kAdjustParamater)];
    zuneiView.backgroundColor = [UIColor whiteColor];
    [self.backGroupScrollView addSubview:zuneiView];
    UITapGestureRecognizer *tapGes1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToMember:)];
    [zuneiView addGestureRecognizer:tapGes1];
    
    
    UILabel *zuNeiChengYuanLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 10 * kAdjustParamater, 100 * kAdjustParamater, 15 * kAdjustParamater)];
    zuNeiChengYuanLabel.text = @"组内成员";
    zuNeiChengYuanLabel.textColor = [UIColor blackColor];
    zuNeiChengYuanLabel.textAlignment  = NSTextAlignmentLeft;
    zuNeiChengYuanLabel.font = [UIFont systemFontOfSize:16 * kAdjustParamater];
    [zuneiView addSubview:zuNeiChengYuanLabel];
    // 组内人数
    self.groupMemberCount = [[UILabel alloc] initWithFrame:CGRectMake(305 * kAdjustParamater, 11 * kAdjustParamater, 40 * kAdjustParamater, 12 * kAdjustParamater)];
    self.groupMemberCount.text = @"100人";
    self.groupMemberCount.textAlignment = NSTextAlignmentCenter;
    self.groupMemberCount.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    self.groupMemberCount.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
    [zuneiView addSubview:_groupMemberCount];
    
    // 灰色箭头
    UIImageView *grayJianTouOnMemberCountRight = [[UIImageView alloc] initWithFrame:CGRectMake(self.groupMemberCount.right + 5 * kAdjustParamater, self.groupMemberCount.top - 2 * kAdjustParamater, 17 * kAdjustParamater, 17 * kAdjustParamater)];
    grayJianTouOnMemberCountRight.image = [UIImage imageNamed:@"grayJianTou"];
    [zuneiView addSubview:grayJianTouOnMemberCountRight];
    
    // 组内成员下方灰色条
    UILabel *grayLineOnZuNeiBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, zuneiView.bottom, kScreenWidth, 1 * kAdjustParamater)];
    grayLineOnZuNeiBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.backGroupScrollView addSubview:grayLineOnZuNeiBottom];
    
    // 成员详情背景
    UIView *memberDetailBackground = [[UIView alloc] initWithFrame:CGRectMake(0, grayLineOnZuNeiBottom.bottom, kScreenWidth, 90 * kAdjustParamater)];
    memberDetailBackground.backgroundColor = [UIColor whiteColor];
    memberDetailBackground.userInteractionEnabled = YES;
    [self.backGroupScrollView addSubview:memberDetailBackground];
    
    UITapGestureRecognizer *memberDetailBackgroundTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(memberDetailGes:)];
    [memberDetailBackground addGestureRecognizer:memberDetailBackgroundTapGes];
    
    for (int i = 0; i < self.groupMemberArray.count; i++) {
        NSDictionary *tempDic = self.groupMemberArray[i];
        // 构建头像
        UIImageView *tempImage = [[UIImageView alloc] initWithFrame:CGRectMake(15 * kAdjustParamater + 59 * kAdjustParamater * i, 15 * kAdjustParamater, 44 * kAdjustParamater, 44 * kAdjustParamater)];
        tempImage.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        tempImage.layer.cornerRadius = 22 * kAdjustParamater;
        tempImage.layer.masksToBounds = YES;
        [memberDetailBackground addSubview:tempImage];
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(tempImage.left - 8 * kAdjustParamater, tempImage.bottom + 8 * kAdjustParamater, 60 * kAdjustParamater, 12 * kAdjustParamater)];
      //  nameLabel.backgroundColor = [UIColor redColor];
        nameLabel.text = [tempDic valueForKey:@"name"];
        nameLabel.textColor = [UIColor blackColor];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
        [memberDetailBackground addSubview:nameLabel];
    }
    
    // 成员详情下方灰色条
    UILabel *grayLineOnMemberDetailBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, memberDetailBackground.bottom, kScreenWidth, 10 * kAdjustParamater)];
    grayLineOnMemberDetailBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    [self.backGroupScrollView addSubview:grayLineOnMemberDetailBottom];
    
    // 小组介绍
    UIView *groupIntroduceBackground = [[UIView alloc] initWithFrame:CGRectMake(0, grayLineOnMemberDetailBottom.bottom, kScreenWidth, 35 * kAdjustParamater)];
    groupIntroduceBackground.backgroundColor = [UIColor whiteColor];
    [self.backGroupScrollView addSubview:groupIntroduceBackground];
    
    UILabel *groupIntroduceLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 10 * kAdjustParamater, 100 * kAdjustParamater, 15 * kAdjustParamater)];
    groupIntroduceLabel.text = @"小组介绍";
    groupIntroduceLabel.textColor = [UIColor blackColor];
    groupIntroduceLabel.textAlignment  = NSTextAlignmentLeft;
    groupIntroduceLabel.font = [UIFont systemFontOfSize:16 * kAdjustParamater];
    [groupIntroduceBackground addSubview:groupIntroduceLabel];
    
    // 小组介绍下方的灰色条
    UILabel *grayLineOnIntroduceBackGroundBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, groupIntroduceBackground.bottom, kScreenWidth, 1 * kAdjustParamater)];
    grayLineOnIntroduceBackGroundBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.backGroupScrollView addSubview:grayLineOnIntroduceBackGroundBottom];
    
    // 小组创建时间
    self.groupCreateTime = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, grayLineOnIntroduceBackGroundBottom.bottom + 10 * kAdjustParamater, 150 * kAdjustParamater, 12 * kAdjustParamater)];
 //   self.groupCreateTime.backgroundColor = [UIColor redColor];
    self.groupCreateTime.text = @"该小组创建于2016.1.1";
    self.groupCreateTime.textAlignment = NSTextAlignmentLeft;
    self.groupCreateTime.textColor = [UIColor blackColor];
    self.groupCreateTime.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
    [self.backGroupScrollView addSubview:_groupCreateTime];
    
    // 小组介绍详情
    self.groupIntroduceDetail = [[UILabel alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, self.groupCreateTime.bottom + 10 * kAdjustParamater, 345 * kAdjustParamater, 50 * kAdjustParamater)];
    self.groupIntroduceDetail.text = @"大师傅给为欧文和父亲问非全五分我发四女偶为接哦我就分为激发的少年班赛欧的你阿斯顿发送到交工前我Ian搜不过闹死偶发咨询丑女弄is的Fiona山东干";
    self.groupIntroduceDetail.textColor = [UIColor blackColor];
    self.groupIntroduceDetail.textAlignment = NSTextAlignmentLeft;
    self.groupIntroduceDetail.numberOfLines = 0;
    self.groupIntroduceDetail.font = [UIFont systemFontOfSize:13 * kAdjustParamater];
    // 自适应高度
    CGRect newFrame = self.groupIntroduceDetail.frame;
    newFrame.size.height = [[NetStatusHandle shareHandle] adjustHeight:self.groupIntroduceDetail.text size:13 width:345];
    self.groupIntroduceDetail.frame = newFrame;
    [self.backGroupScrollView addSubview:_groupIntroduceDetail];
    
    // 小组介绍详情下方的灰色条
    UILabel *grayLineOnGroupIntroduceDetailBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, self.groupIntroduceDetail.bottom + 10 * kAdjustParamater, kScreenWidth, 10 * kAdjustParamater)];
    grayLineOnGroupIntroduceDetailBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    [self.backGroupScrollView addSubview:grayLineOnGroupIntroduceDetailBottom];
    
    
    // 申请入组button
    self.joinGroupButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.joinGroupButton.frame = CGRectMake((375 - 265) / 2 * kAdjustParamater, grayLineOnGroupIntroduceDetailBottom.bottom + 35 * kAdjustParamater, 265 * kAdjustParamater, 40 * kAdjustParamater);
    self.joinGroupButton.backgroundColor = [UIColor colorWithRed:84 / 255.0 green:199 / 255.0 blue:222 / 255.0 alpha:1];
    self.joinGroupButton.layer.cornerRadius = 6 * kAdjustParamater;
    [self.joinGroupButton setTitle:@"申 请 入 组" forState:UIControlStateNormal];
    [self.joinGroupButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.joinGroupButton.titleLabel.font = [UIFont systemFontOfSize:20 * kAdjustParamater];
    [self.joinGroupButton addTarget:self action:@selector(wantToJoinGroup:) forControlEvents:UIControlEventTouchUpInside];
    [self.backGroupScrollView addSubview:_joinGroupButton];
    
 //   NSLog(@"bottom = %f", self.backGroupScrollView.bottom);
    self.backGroupScrollView.contentSize = CGSizeMake(kScreenWidth, self.joinGroupButton.bottom);
    
   // NSLog(@"height = %f", self.backGroupScrollView.contentSize.height);
}

#pragma mark - 申请入组的点击方法
- (void)wantToJoinGroup:(UIButton *)button
{
    NSLog(@"点击入组");
//    GroupIDCheckViewController *groupIDVC = [[GroupIDCheckViewController alloc] init];
//    [self.navigationController pushViewController:groupIDVC animated:YES];
//    
    FriendIDCheckViewController *friendIDVC = [[FriendIDCheckViewController alloc] init];
    [self.navigationController pushViewController:friendIDVC animated:YES];
}

#pragma mark - 返回上一个界面
- (void)returnToLastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 组内成员
- (void)goToMember:(UITapGestureRecognizer *)tapGes
{
    GroupMemeberViewController *groupMemberVC = [[GroupMemeberViewController alloc] init];
    [self.navigationController pushViewController:groupMemberVC animated:YES];
}

#pragma mark - 成员详情进入组内
- (void)memberDetailGes:(UITapGestureRecognizer *)tapGes
{
    GroupMemeberViewController *groupMemberVC = [[GroupMemeberViewController alloc] init];
    [self.navigationController pushViewController:groupMemberVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
